<template>
  <div>
    <el-button size="mini" plain icon="el-icon-upload" @click="daoru">导入</el-button>
    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog title="导入" :visible.sync="dialogVisible" width="500px" append-to-body v-if="dialogVisible">
      <!-- 文件上传-->
      <el-form :model="dataForm" ref="dataForm" label-width="150px" @submit.native.prevent>
        <!-- <el-form-item label="模板下载" prop="finalIsReturn">
          <el-button size="mini" @click="downLoadTemplateFile">
            导入模板下载
          </el-button>
        </el-form-item> -->
        <el-form-item label="店铺：" prop="name">
          <!-- <el-select
            v-model="dataForm.name"
            placeholder="店铺"
            clearable
            filterable
            remote
            :remote-method="getselectShopList"
            @change=""
          >
            <el-option
              v-for="dict in cascadeList"
              :key="dict.id"
              :label="dict.storeName"
              :value="dict.shopId"
            />
          </el-select> -->
          <!-- <el-select
            filterable
            remote
            :remote-method="getselectShopList"
            v-model="dataForm.name"
            value-key="shopId"
            @change="handleShopChange"
          >
            <el-option
              v-for="dict in cascadeList"
              :key="dict.id"
              :label="dict.storeName"
              :value="dict"
            />
          </el-select> -->
          <!-- <el-input
            type="text"
            v-model.trim="dataForm.name"
            clearable
          ></el-input> -->
          <el-cascader class="region" v-model="dataForm.name" placeholder="选择店铺分组" :options="cascadeList"
            :props="{ checkStrictly: true }" style="width: 300px; height: 30px">
          </el-cascader>
        </el-form-item>
        <el-form-item label="文件：" prop="file">
          <el-upload class="upload-import" ref="uploadImport" :http-request="httpRequest" action=""
            :on-remove="handleRemove" :file-list="fileList" :limit="1" :on-change="handleChange" :auto-upload="false"
            :before-upload="beforeAvatarUpload">
            <!--
            accept="application/zip,.zip" 设置接收的文件类型-->
            <el-button v-show="!hasFile" slot="trigger" size="small" type="primary">选取文件</el-button>
            <!-- <div slot="tip" class="el-upload__tip">
          只能上传zip文件，且不超过10M
        </div> -->
          </el-upload>
          <!-- 关闭自动上传-->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitUpload">提交</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  listBiPddPromotionDwd,
  getBiPddPromotionDwd,
  delBiPddPromotionDwd,
  addBiPddPromotionDwd,
  updateBiPddPromotionDwd,
  selectShopList,
} from "@/api/pdd/BiPddPromotionDwd";
//--------导入token
import { getToken } from "@/utils/auth";
import axios from "axios";
export default {
  name: "BiOrderAftersales",
  props: {
    uploadUrl: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      hasFile: false,
      fileList: [],
      dialogVisible: false,
      pjtUrl: process.env.VUE_APP_BASE_API,

      cascadeList: [],
      dataForm: {
        name: null,
        file: null,
        shopName: null,
      },
    };
  },
  watch: {
    // uploadUrl: {
    //   handler: function (newnew, oldold) {
    //     this.Upload.url = process.env.VUE_APP_BASE_API + this.uploadUrl;
    //   },
    //   deep: true,
    //   immediate: true,
    // },
  },
  methods: {
    handleShopChange(selectedObj) {
      // 直接获取完整对象
      console.log("选中店铺详情：", selectedObj);
      console.log("店铺ID：", selectedObj.shopId);
      console.log("店铺名：", selectedObj.storeName);
      this.dataForm.name = selectedObj.shopId;
      this.dataForm.shopName = selectedObj.storeName;
    },
    getselectShopList(key) {
      selectShopList({ storeName: key }).then((response) => {
        this.cascadeList = response;
      });
    },
    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      // const isJPG = file.type == 'image/jpeg' || file.type == 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 100;
      // if (!isJPG) {
      //     this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      // }
      if (!isLt2M) {
        this.$message.error("上传导入文件大小不能超过 100MB!");
      }

      return isLt2M;
    },
    handleRemove(file, fileList) {
      if (!fileList.length) {
        this.hasFile = false;
      }
      this.dataForm.file = null;
    },
    handleChange(file, fileList) {
      if (fileList.length >= 2) {
        return;
      }
      if (fileList.length === 1) {
        this.hasFile = true;
      }
      this.dataForm.file = file;
    },
    //确定按钮
    //调用组件upload的submit方法，从而触发httpRequest方法，实现手动上传
    submitUpload() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$refs.uploadImport.submit();
        }
      });
    },
    httpRequest(param) {
      if (this.dataForm.name.length < 3) {
        this.$modal.msgWarning("请选择到店铺");
        return;
      }
      let fd = new FormData();
      fd.append("file", param.file); // 传文件
      // fd.append("salesGrade", this.dataForm.name);
      //dataPar.file.raw
      console.log(JSON.stringify(fd));
      this.openLoading();
      axios
        .post(
          process.env.VUE_APP_BASE_API +
          this.uploadUrl +
          "?shopId=" +
          this.dataForm.name +
          "&shopName=" +
          this.dataForm.shopName,
          fd,
          {
            headers: {
              header: { responseType: "blob" },

              Authorization: "Bearer " + getToken(),
              "Content-Type": "multipart/form-data",
            }, //定义内容格式,很重要
            // timeout: 20000,
          }
        )
        .then((response) => {
          this.$modal.closeLoading();
          this.hasFile = false;
          this.dialogVisible = false;
          console.log(response);

          if (response.data.code == 200) {
            this.$modal.msgSuccess("导入成功");

            this.$emit("handleSuccess");
          } else {
            this.$modal.msgError(response.data.msg);
          }
        });
    },
    //部门文件导入修改打开
    daoru() {
      this.getselectShopList();
      this.dialogVisible = true;
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在导入，请稍候！");
    },
  },
};
</script>